package cn.itsource.petHome.org.controller;

import cn.itsource.petHome.org.domain.Department;
import cn.itsource.petHome.org.query.DepartmentQuery;
import cn.itsource.petHome.org.service.IDepartmentService;
import cn.itsource.petHome.utils.AjaxResult;
import cn.itsource.petHome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@CrossOrigin
public class DepartmentController {
    @Autowired
    private IDepartmentService service;
    @DeleteMapping(value = "/{id}")//发起delete请求
    public AjaxResult delete(@PathVariable("id") Long id){
        service.delete(id);
        return AjaxResult.me();
    }

    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        service.batchDelete(ids);
        return AjaxResult.me();
    }


    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        if(department.getId()==null){
            service.save(department);
        }else{
            service.update(department);
        }

        return AjaxResult.me();
    }




    @GetMapping
    public List<Department> selectAll(){

        return service.loadAll();
    }

    @PostMapping
    public PageResult<Department> selectQuery(@RequestBody DepartmentQuery q){
        return service.queryByPage(q);
    }
}
